import { nearest_data } from "../lib/nearest.mjs";
import datarover from "../lib/datarover.mjs";

export default function datahelper({ container, template }) {
  console.log({ container, template, data: nearest_data(container) });

  if (typeof template?.data === "function") {
    console.log("template.data is a function");
    return template.data({
      container,
      template,
      data: nearest_data(container),
    });
  }
  // 2. 直接数据 + 可选数据路径
  else if (template?.data !== undefined) {
    console.log("template.data is not a function");
    if (template?.datapath) {
      return datarover(template.data, template.datapath);
    } else return template.data;
  }
  // 3. 从容器获取数据路径
  else if (template?.datapath !== undefined) {
    console.log("template.datapath is not undefined");
    return datarover(container, template.datapath);
  } else {
    console.log("template.datapath is undefined");
    return nearest_data(container);
  }
}
